(function($) {
	var storePull,sourceData;
	var page = 1,Limit = 20;
	var pulling = false;
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function getStoreData(){
		if (pulling) {
			storePull.endPullUpToRefresh()
			return;
		}
		pulling = true
		setTimeout(function(){
			var dataArr = [{'name':'新员工培训'+page,'date':'2021-09-10 10:01','isDir':'0'},
					{'name':'内科护理学'+page,'date':'2021-09-10 10:02','isDir':'0'},
					{'name':'护理人员分层级培训'+page,'date':'2021-09-10 10:03','isDir':'0'},
					{'name':'护理人员聘用管理办法'+page,'date':'2021-09-10 10:04','isDir':'0'},
					{'name':'护理人员弹性调配制'+page,'date':'2021-09-10 10:05','isDir':'0'},
					{'name':'护理人员配置原则'+page,'date':'2021-09-10 10:05','isDir':'0'},
					{'name':'临床护理排班原则'+page,'date':'2021-09-10 10:05','isDir':'0'},
					{'name':'护理人员替代制度'+page,'date':'2021-09-10 10:05','isDir':'0'}]
			if (page == 1) {
				sourceData = dataArr;
			}else{
				sourceData = sourceData.concat(dataArr)
			}
			storePull.endPullUpToRefresh();
			if (page > 2) {
				storePull.endPullUpToRefresh(true);
			}
			updateUI(dataArr)
			page += 1;
			pulling = false
		},800);
	}
	function updateUI(dataArr){
		var scroll = $("#scroll")[0];
		var count = scroll.querySelectorAll('.itemArea').length
		var tipsDom = $('#scroll .mui-pull-bottom-tips')[0]
		var icon='nur-shipin'
		if (page%2==0) {
			icon='nur-doc'
		}
		var isAllSel= false;
		if ($('#selectAllBtn')[0].getAttribute('class').indexOf('mui-active') != -1) {
			isAllSel = true
		}
		for(var i = 0; i < dataArr.length; i++){
			var oneData = dataArr[i]
			var html =  '<div class="nurfont '+icon+' itemImg"></div>'+
				'<div class="itemText">'+oneData['name']+'</div>'+
				'<div class="itemDate">2021-09-09 10:59</div>'+
				'<div class="nurfont nur-check-none selectIcon"></div>'
			var itemArea = document.createElement('div')
			itemArea.setAttribute('class','itemArea')
			itemArea.id='file-'+(count+i);
			itemArea.innerHTML = html
			if (isAllSel) {
				itemArea.classList.add('mui-active')
			}
			scroll.insertBefore(itemArea,tipsDom)
			itemArea.addEventListener('tap',itemTapped) //查看结果
		}
	}
	function itemTapped(){
		if (document.body.getAttribute('class').indexOf('editing') != -1) {
			if (this.getAttribute('class').indexOf('mui-active') != -1) {
				this.classList.remove('mui-active')
			}else{
				this.classList.add('mui-active')
			}
			return
		}
		
		var webview = plus.webview.create('trainVideo.html', 'trainVideo', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{});
		webview.show('fade-in');
	}
	
	function baseSetup() {
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		
		mui("body").on("tap", ".searchArea .mui-icon-clear", function() {
			if ($('#search')[0] != "") {
				$('#search')[0].value = ""
			}
			resetStoreRefresh()
		});
		var search =  $('#search')[0];
		search.addEventListener('input',function(){
			resetStoreRefresh()
		});
		var scroll = $('#scroll')[0];
		storePull = $(scroll).pullToRefresh({
			up: {
				callback: function() {
					getStoreData()
				}
			}
		});
		//编辑
		mui(".mui-bar").on("tap", "#editBtn", editBtnTapped);
		//全选
		mui(".bottomArea").on("tap", "#selectAllBtn",selectAllBtnTapped);
		//删除
		mui(".bottomArea").on("tap", "#deleteBtn", function() {
			mui('.itemArea.mui-active').each(function() {
				$('#scroll')[0].removeChild(this)
			});
			editBtnTapped()
			resetStoreRefresh();
			
		});
		getStoreData();	
	}
	function editBtnTapped(){
		var editBtn = $('#editBtn')[0]
		var bodyDom = document.body
		if (bodyDom.getAttribute('class').indexOf('editing') != -1) {
			bodyDom.classList.remove('editing')
			$('#selectAllBtn')[0].classList.remove('mui-active');
			mui('.itemArea').each(function() {
				this.classList.remove('mui-active');
			});
			editBtn.innerHTML = '编辑'
		}else{
			bodyDom.classList.add('editing')
			editBtn.innerHTML = '取消'
		}
	}
	
	function selectAllBtnTapped(){
		var selectAllBtn = $('#selectAllBtn')[0]
		if (selectAllBtn.getAttribute('class').indexOf('mui-active') != -1) {
			selectAllBtn.classList.remove('mui-active')
			mui('.itemArea').each(function() {
				this.classList.remove('mui-active');
			});
		}else{
			selectAllBtn.classList.add('mui-active')
			mui('.itemArea').each(function() {
				this.classList.add('mui-active');
			});
		}
	}
	
	function resetStoreRefresh(){
		page = 1;
		sourceData = []
		if (storePull) {
			storePull.refresh(true)
		}
		getStoreData();
		mui('.itemArea').each(function() {
			$('#scroll')[0].removeChild(this)
		});
		$('#wrapper').scroll().scrollTo(0,0,100);
	}
})(mui);